package com.wind.gateway.config;

import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import org.springframework.util.StringUtils;

import java.util.Properties;

/**
 * @author wind
 * @date 2020/6/15
 */
public class NacosDataSourceFactoryBean extends com.alibaba.cloud.sentinel.datasource.factorybean.NacosDataSourceFactoryBean {
    private String username;
    private String password;
    @Override
    public NacosDataSource getObject() throws Exception {
        Properties properties = new Properties();
        if (!StringUtils.isEmpty(getServerAddr())) {
            properties.setProperty("serverAddr", getServerAddr());
            properties.setProperty("username",this.username);
            properties.setProperty("password",this.password);
        } else {
            properties.setProperty("accessKey", getAccessKey());
            properties.setProperty("secretKey", getSecretKey());
            properties.setProperty("endpoint", getEndpoint());
        }

        if (!StringUtils.isEmpty(getNamespace())) {
            properties.setProperty("namespace", getNamespace());
        }

        return new NacosDataSource(properties, getGroupId(), getDataId(), getConverter());
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
